package edu.bath.sensorvisualiser;

import edu.bath.sensorframework.Visualisation;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;

public class Seg7 implements Visualisation<Float> {
	private Canvas c;
	private Float data = 0.0f;
	
	private int fgColour;
	private int bgColour;
	private int polysx [] [] = {
			{1, 2, 8, 9, 8, 2}, // ​​segment 0
			{9,10,10, 9, 8, 8}, // ​​segment 1
			{9,10,10, 9, 8, 8}, // ​​segment 2
			{1, 2, 8, 9, 8, 2}, // ​​Segment 3
			{1, 2, 2, 1, 0, 0}, // ​​Segment 4
			{1, 2, 2, 1, 0, 0}, // ​​Segment 5
			{1, 2, 8, 9, 8, 2}, // ​​6 segment
	};
	private int polysy [] [] = {
			{1, 0, 0, 1, 2, 2}, // ​​segment 0
			{1, 2, 8, 9, 8, 2}, // ​​segment 1
			{9,10,16,17,16,10}, // ​​segment 2
			{17,16,16,17,18,18}, // ​​Segment 3
			{9,10,16,17,16,10}, // ​​Segment 4
			{1, 2, 8, 9, 8, 2}, // ​​Segment 5
			{9, 8, 8, 9,10,10}, // ​​6 segment
	};
	private int digits [] [] = {
			{1,1,1,1,1,1,0}, // ​​number 0
			{0,1,1,0,0,0,0}, // ​​Number 1
			{1,1,0,1,1,0,1}, // ​​Point 2
			{1,1,1,1,0,0,1}, // ​​Number 3
			{0,1,1,0,0,1,1}, // ​​Number 4
			{1,0,1,1,0,1,1}, // ​​item 5
			{1,0,1,1,1,1,1}, // ​​Number 6
			{1,1,1,0,0,0,0}, // ​​number 7
			{1,1,1,1,1,1,1}, // ​​number 8
			{1,1,1,1,0,1,1}, // Number 9
			{0,0,0,0,0,0,1}  // minus sign
	};

	public Seg7(Canvas c, int bg, int fg) {
		fgColour = fg;
		bgColour = bg;
		this.c = c;
	}
	
	@Override
	public void draw() {
		// Find out how many digits the data is
		String numberString = data.toString();
		int values[] = new int[numberString.length()];
		int cells = 0;
		for(int n = 0 ; n < numberString.length() ; n++) {
			char c = numberString.charAt(n);
			if(c == '-') {
				values[n] = 10;
				cells++;
			} else if(c == '.') {
				values[n] = 11;
			} else {
				values[n] = Character.digit(c, 10);
				cells++;
			}
		}

		// Fill the background
		c.drawColor(bgColour);
		
		// Draw the digits
		int count = 0;
		for(int n = 0 ; n < values.length ; n++) {
			int val = values[n];
			int xOffset =  (c.getWidth() / cells)*(count);
			// Calculate some values
			int dx = c.getWidth() / cells / 10 - 1;
			int dy = c.getHeight() / 18;
			
			// Draw segments
			Paint p = new Paint();
			p.setStyle(Style.FILL);
			p.setColor(fgColour);
			for(int i = 0 ; i < 7 ; ++i) {
				if(digits[val][i] == 1) {
					Path polygon = new Path();
					polygon.moveTo(dx*polysx[i][5]+xOffset, dy*polysy[i][5]); // Move to end of shape, so we don't get a line going back to (0,0)
					for(int j = 0 ; j < 6 ; ++j) {
						polygon.lineTo(dx*polysx[i][j]+xOffset, dy*polysy[i][j]);
					}
					polygon.close();
					c.drawPath(polygon, p);
				}
			}

			// Draw dividing lines
			Paint bgP = new Paint();
			bgP.setColor(bgColour);
			bgP.setStrokeWidth(2.0f);
			c.drawLine(0+xOffset, 0, dx*10+xOffset, dy*10, bgP);
			c.drawLine(0+xOffset, 8*dy, 10*dx+xOffset, 18*dy, bgP);
			c.drawLine(0+xOffset, 10*dy, 10*dx+xOffset, 0, bgP);
			c.drawLine(0+xOffset, 18*dy, 10*dx+xOffset, 8*dy, bgP);
			
			// Draw decimal point
			if((n+1) < values.length && values[n+1]==11) {
				// draw a circle!
				c.drawCircle(dx*polysx[3][0]+dx*(polysx[3][2]+1)+xOffset, dy*(polysy[3][0]+1), 3, p);
				n++;
			}
			count++;
		}
	}

	@Override
	public void addData(Float data) {
		this.data = data;
	}

	@Override
	public void flushData() {
		this.data = 0.0f;
	}
}
